"""
@File    :   router.py
@Time    :   2024/03/08 19:21:38
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   OAuth2密码模式认证相关路由
"""

from typing import Annotated

from fastapi import APIRouter, Depends

from src.business.admin.system.oauth2.auth.captcha.dependencies.no_introduction import (
    depend_validate_and_del_image_captcha,
    depend_validate_and_del_sliding_captcha,
)
from src.business.admin.system.oauth2.auth.captcha.enums import CaptchaTypeEnum
from src.business.admin.system.oauth2.auth.password.dependencies.route_depend import (
    route_depend_password_create_double_token,
    route_depend_swagger_password_create_double_token,
)
from src.business.admin.system.oauth2.token.schemas.out import (
    DoubleTokenCompleteOutSchema,
)
from src.config import get_app_settings
from src.utils.unified_response import SuccessApiJSONResponse

# 创建路由
router = APIRouter(
    prefix="/admin/system/oauth2/auth/password",
    tags=[
        "后台OAuth2密码模式认证接口",
    ],
)

# 获取应用配置
settings = get_app_settings()

# 只在开发(debug)模式下开启swagger专用的请求认证
if settings.debug:

    @router.post("/swagger-authiruze", summary="swagger专用的请求认证")
    async def route_swagger_authiruze(
        double_token: Annotated[
            DoubleTokenCompleteOutSchema,
            Depends(route_depend_swagger_password_create_double_token),
        ],
    ) -> SuccessApiJSONResponse:
        """swagger专用的OAuth2密码模式请求认证(无需验证码, 密码不加密)

        Args:
            double_token (Annotated[ tuple[AccessTokenSchema, RefreshTokenSchema], Depends): 获取双Token的依赖

        Returns:
            SuccessApiJSONResponse: 响应信息
        """
        # 拆分出访问令牌与刷新令牌
        access_token, refresh_token = (
            double_token.access_token,
            double_token.refresh_token,
        )
        data = {
            "access_token": access_token.access_token,
            "access_token_expiration_datetime": access_token.expiration_datetime,
            "refresh_token": refresh_token.refresh_token,
            "refresh_token_expiration_datetime": refresh_token.expiration_datetime,
            "token_type": access_token.token_type,
        }
        return SuccessApiJSONResponse(
            message="swagger专用的OAuth2密码模式请求认证成功", data=data
        )


# 根据是否开启验证码来解锁对应的路由
if (
    settings.enable_user_login_captcha
    and settings.user_login_captcha_type == CaptchaTypeEnum.image
):
    # 使用图像验证码
    @router.post("/authiruze", summary="请求认证(需要图像验证码)")
    async def route_username_password_image_captcha_authiruze(
        image_captcha_validate_result: Annotated[
            bool, Depends(depend_validate_and_del_image_captcha)
        ],
        double_token: Annotated[
            DoubleTokenCompleteOutSchema,
            Depends(route_depend_password_create_double_token),
        ],
    ):
        # 拆分出访问令牌与刷新令牌
        access_token, refresh_token = (
            double_token.access_token,
            double_token.refresh_token,
        )
        data = {
            "access_token": access_token.access_token,
            "access_token_expiration_datetime": access_token.expiration_datetime,
            "refresh_token": refresh_token.refresh_token,
            "refresh_token_expiration_datetime": refresh_token.expiration_datetime,
            "token_type": access_token.token_type,
        }
        return SuccessApiJSONResponse(message="OAuth2密码模式请求认证成功", data=data)
elif (
    settings.enable_user_login_captcha
    and settings.user_login_captcha_type == CaptchaTypeEnum.sliding_puzzle
):
    # 使用滑块验证码
    @router.post("/authiruze", summary="请求认证(需要滑块验证码)")
    async def route_username_password_sliding_captcha_authiruze(
        sliding_captcha_validate_result: Annotated[
            bool, Depends(depend_validate_and_del_sliding_captcha)
        ],
        double_token: Annotated[
            DoubleTokenCompleteOutSchema,
            Depends(route_depend_password_create_double_token),
        ],
    ):
        # 拆分出访问令牌与刷新令牌
        access_token, refresh_token = (
            double_token.access_token,
            double_token.refresh_token,
        )
        data = {
            "access_token": access_token.access_token,
            "access_token_expiration_datetime": access_token.expiration_datetime,
            "refresh_token": refresh_token.refresh_token,
            "refresh_token_expiration_datetime": refresh_token.expiration_datetime,
            "token_type": access_token.token_type,
        }
        return SuccessApiJSONResponse(message="OAuth2密码模式请求认证成功", data=data)
else:
    # 不使用验证码
    @router.post("/authiruze", summary="请求认证")
    async def route_username_password_authiruze(
        double_token: Annotated[
            DoubleTokenCompleteOutSchema,
            Depends(route_depend_password_create_double_token),
        ],
    ):
        # 拆分出访问令牌与刷新令牌
        access_token, refresh_token = (
            double_token.access_token,
            double_token.refresh_token,
        )
        data = {
            "access_token": access_token.access_token,
            "access_token_expiration_datetime": access_token.expiration_datetime,
            "refresh_token": refresh_token.refresh_token,
            "refresh_token_expiration_datetime": refresh_token.expiration_datetime,
            "token_type": access_token.token_type,
        }
        return SuccessApiJSONResponse(message="OAuth2密码模式请求认证成功", data=data)
